/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmlparser;

/**
 *
 * @author pbelov
 */
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import base.*;

public class userParser extends DefaultHandler{

    private int n = -1;
    User user[] = new User[8];

    String thisElement = "";
    private String DBG = "[DBG]";

    public static String USER = "user";
    public static String NAME = "name";
    public static String LOGIN = "login";
    public static String PRIV = "priv";
    public static String MAIL = "mail";
    public static String TEAM = "team";
    public static String TEAMLEADER = "teamleader";


    public User[] getResult(){
      return user;
    }

    @Override
    public void startDocument() throws SAXException {
      System.out.println("Start parse XML...");
    }

    @Override
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
      System.out.println("[" + qName + "]");
        thisElement = qName;
    }

    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
      thisElement = "";
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
      if (thisElement.equals(USER)) {
          n++;
          //user = new User[n + 1];
          System.out.println(DBG + "new user: " + n);
          user[n] = new User();
      }

      if (thisElement.equals(PRIV)) {
         user[n].setPriv(new Integer(new String(ch, start, length)));
         System.out.println(DBG + PRIV + " = " + user[n].getPrivileges());
      }

      if (thisElement.equals(NAME)) {
         user[n].setName(new String(ch, start, length));
         System.out.println(DBG + NAME + " = " + user[n].getName());
      }

      if (thisElement.equals(LOGIN)) {
         user[n].setLogin(new String(ch, start, length));
         System.out.println(DBG + LOGIN + " = " + user[n].getLogin());
      }

      if (thisElement.equals(MAIL)) {
         user[n].setMail(new String(ch, start, length));
         System.out.println(DBG + MAIL + " = " + user[n].getMail());
      }

      if (thisElement.equals(TEAM)) {
         user[n].setTeamName(new String(ch, start, length));
         System.out.println(DBG + TEAM + " = " + user[n].getTeamName());
      }

      if (thisElement.equals(TEAMLEADER)) {
          user[n].setTeamLeader(new String(ch, start, length));
          System.out.println(DBG + TEAMLEADER + " = " + user[n].getTeamLeader());
      }
      //System.out.println("bug: ID = " + bugs[inc].getID() + " title = " + bugs[inc].getTitle());

    }

    @Override
    public void endDocument() {
      System.out.println(DBG + "Users count = " + user.length);
      System.out.println("Stop parse XML...");
    }
}


